package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysUserYeji;
import com.ruoyi.system.service.ISysUserYejiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.core.domain.entity.SysUser;

/**
 * 员工业绩Controller
 * 
 * @author xyz
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/system/yeji")
public class SysUserYejiController extends BaseController
{
    @Autowired
    private ISysUserYejiService sysUserYejiService;
    
    @Autowired
    private ISysUserService userService;

    /**
     * 查询员工业绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:yeji:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserYeji sysUserYeji)
    {
        startPage();
        List<SysUserYeji> list = sysUserYejiService.selectSysUserYejiList(sysUserYeji);
        return getDataTable(list);
    }

    /**
     * 导出员工业绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:yeji:export')")
    @Log(title = "员工业绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserYeji sysUserYeji)
    {
        List<SysUserYeji> list = sysUserYejiService.selectSysUserYejiList(sysUserYeji);
        ExcelUtil<SysUserYeji> util = new ExcelUtil<SysUserYeji>(SysUserYeji.class);
        util.exportExcel(response, list, "员工业绩数据");
    }

    /**
     * 获取员工业绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:yeji:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysUserYejiService.selectSysUserYejiById(id));
    }

    /**
     * 新增员工业绩
     */
    @PreAuthorize("@ss.hasPermi('system:yeji:add')")
    @Log(title = "员工业绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserYeji sysUserYeji)
    {
        return toAjax(sysUserYejiService.insertSysUserYeji(sysUserYeji));
    }

    /**
     * 修改员工业绩
     */
    @PreAuthorize("@ss.hasPermi('system:yeji:edit')")
    @Log(title = "员工业绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserYeji sysUserYeji)
    {
        return toAjax(sysUserYejiService.updateSysUserYeji(sysUserYeji));
    }

    /**
     * 删除员工业绩
     */
    @PreAuthorize("@ss.hasPermi('system:yeji:remove')")
    @Log(title = "员工业绩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserYejiService.deleteSysUserYejiByIds(ids));
    }

    /**
     * 新增员工业绩记录（带验证）
     */
    @PreAuthorize("@ss.hasPermi('system:yeji:add')")
    @Log(title = "员工业绩", businessType = BusinessType.INSERT)
    @PostMapping("/addWithValidation")
    public AjaxResult addWithValidation(@RequestBody SysUserYeji sysUserYeji)
    {
        // 1. 验证用户名
        if (sysUserYeji.getUserName() == null || sysUserYeji.getUserName().trim().isEmpty()) {
            return error("用户名不能为空");
        }
        
        // 2. 查询并验证用户是否存在
        SysUser user = userService.selectUserByUserName(sysUserYeji.getUserName());
        if (user == null) {
            return error("用户名不存在");
        }
        
        // 3. 设置用户ID和清理后的用户名
        sysUserYeji.setUserId(user.getUserId());
        sysUserYeji.setUserName(sysUserYeji.getUserName().trim());
        
        // 4. 验证日期
        if (sysUserYeji.getAa() == null || sysUserYeji.getAa().trim().isEmpty()) {
            return error("日期不能为空");
        }
        
        // 5. 保存数据
        return toAjax(sysUserYejiService.insertSysUserYeji(sysUserYeji));
    }
}
